package lcc.linkshare;




import java.io.FileNotFoundException;
import java.io.InputStream;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;


public class DBUtils {
	
	public final static String DEFAULT_PROPERTIES = "ConnectionManager.properties";

	private static String DB_DRIVER = null;
	private static  String DB_CONNECTION = null;
	private static String DB_USER = null;
	private static String DB_PASSWORD = null;
	
	static Connection getDBConnection() {
		
		 try {
		      InputStream in = DBUtils.class.getResourceAsStream( DEFAULT_PROPERTIES );
		      if ( in == null )
		        throw new FileNotFoundException( "y no properties file? dc/database/ConnectionManager.properties" );
		      Properties prop = new Properties();
		      prop.load(in);
		      DB_DRIVER= prop.getProperty("DB_DRIVER");
		      DB_CONNECTION = prop.getProperty("DB_CONNECTION");
		      DB_USER= prop.getProperty("DB_USER");
		      DB_PASSWORD= prop.getProperty("DB_PASSWORD");
		      
		    } catch (Exception ex) {
		      System.out.println("Exception while intitializing OracleConnectionManager"+ex);
		    }
		 
		Connection dbConnection = null;
 
		try {
			Class.forName(DB_DRIVER);
		} catch (ClassNotFoundException e) {
			System.out.println(e.getMessage());
		}
 
		try { 
			
			dbConnection = DriverManager.getConnection(DB_CONNECTION, DB_USER,DB_PASSWORD);
			return dbConnection;
		} catch (SQLException e) {
			System.out.println(e.getMessage());
		}
 
		return dbConnection;
	}
	
	static void cleanUp(Connection conn, CallableStatement cStmt) {
		
		try {
			if (cStmt != null) {
				cStmt.close();
			}
			if (conn != null) {
				conn.close();
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
}
